import { DataSource, DataSourceOptions } from 'typeorm';

// mysql开发环境配置
export const devConfig: DataSourceOptions = {
  type: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  username: 'yang',
  password: '123456',
  database: 'my_database',
  synchronize: true,
};

// 测试环境配置
const stagingConfig: DataSourceOptions = {
  type: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  username: 'yang',
  password: '123456',
  database: 'my_database',
  synchronize: true,
};

// 生产环境配置
const prodConfig: DataSourceOptions = {
  type: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  username: 'yang',
  password: '123456',
  database: 'my_database',
  synchronize: true,
};

export const getCurrentEnvConfig = (): DataSourceOptions => {
  if (process.env.NODE_ENV?.startsWith('dev')) {
    return devConfig;
  } else if (process.env.NODE_ENV?.startsWith('staging')) {
    return stagingConfig;
  } else if (process.env.NODE_ENV?.startsWith('prod')) {
    return prodConfig;
  }
};

const AppDataSource = new DataSource({
  ...getCurrentEnvConfig(),
  entities: ['./src/**/entities/*.entity{.js,.ts}'],
  migrations: [`./migrations/${process.env.NODE_ENV}/*.{j,t}s`],
});

export default AppDataSource;
